package restcrud.handler;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.taglibs.standard.tag.el.sql.UpdateTag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import restcrud.dao.DepartmentDao;
import restcrud.dao.EmployeeDao;
import restcrud.entites.Employee;

@Controller
public class EmployeeHandler {

	@Autowired
	private EmployeeDao employeeDao;
	
	@Autowired
	private DepartmentDao departmentDao;

	/**
	 * @return String
	 */
	@ModelAttribute
	public void startTag(@RequestParam(value="id",required=false) Integer id,Model model) {
		if(id!=null) {
			Employee employee = employeeDao.get(id);
			model.addAttribute("employee", employee);
		}
	}
	/**
	 *  修改员工信息 
	 * @param map
	 * @return String
	 */
	@RequestMapping(value="/add", method=RequestMethod.PUT)
	public String updateEmp(@Valid @ModelAttribute("employee")Employee employee,
			BindingResult result,ModelMap map){
		employeeDao.save(employee);
		System.out.println("修改"+employee);
		//显示错误消息
		if (result.getErrorCount()>0) {
			for (FieldError error : result.getFieldErrors()) {
				System.out.println("输出出错消息"+error.getObjectName()+" "+error.getDefaultMessage());
			}
			//验证错误回显
			map.addAttribute("departments", departmentDao.getDepartments());
			return "Edit";
		}
		return "redirect:/emps";//返回再次查询
	}
	/**
	 *  修改员工信息前的，回显数据
	 * @param map
	 * @return String
	 */
	@RequestMapping(value="/edit/{id}", method=RequestMethod.GET)
	public String editEmp(@PathVariable("id") Integer id,Model model){
		Employee employee = employeeDao.get(id);
		System.out.println("编辑");
		model.addAttribute("employee", employee);//回显数据
		model.addAttribute("departments", departmentDao.getDepartments());	
		return "Edit";
	}
	
	/**
	 *  删除员工信息
	 * @param map
	 * @return String
	 */
	@RequestMapping(value="/del/{id}", method=RequestMethod.DELETE)
	public String delEmp( @PathVariable("id") Integer id){
		employeeDao.delete(id);
		System.out.println("删除");
		return "redirect:/emps";
	}
	
	
	/**
	 *  保存员工信息 
	 * @param map
	 * @return String
	 */
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public String saveEmp(Employee employee,BindingResult result){
		employeeDao.save(employee);
		List<ObjectError> list = result.getAllErrors();
		for (ObjectError objectError : list) {
			System.out.println(objectError);
		}
		System.out.println("保存"+employee);
		return "redirect:/emps";//返回再次查询
	}
	//这两个方法，映射相同实际上可以根据不同入参类型进行选择
	/**
	 * 回显部门表 到select
	 * @param map
	 * @return String
	 */
	@RequestMapping(value="/add", method=RequestMethod.GET)
	public String add(Map<String, Object> map){
		map.put("departments", departmentDao.getDepartments());
		map.put("employee", new Employee());
		return "Edit";
	}
	
	/**
	 * 查询员工总数
	 * @param map
	 * @return String
	 */
	@RequestMapping("/emps")
	public String list(Map<String, Object> map,HttpServletResponse respose){
		map.put("employees", employeeDao.getAll());
		respose.setHeader("Cache", "no-cache");
		return "list";
	}
	
}
